<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>欢迎页面</title>
    <script src="${pageContext.request.contextPath}/js/jquery-3.4.1.min.js"></script>
    <script src="${pageContext.request.contextPath}/js/bootstrap.js"></script>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap.css">
</head>
<body>
<h1>登录成功，欢迎您 ${sessionScope.user.name}</h1>
<h1>${requestScope.msg}</h1>
<hr/>
<c:forEach items="${requestScope.fileNames}" var="file">
    <a href="${pageContext.request.contextPath}/upload/${file}">${file}</a>
    <a href="${pageContext.request.contextPath}/download?fileName=${file}">下载</a>
    <br/>
</c:forEach>
<hr/>
<button id="mybtn1" class="btn btn-info">查询所有</button>
<button id="mybtn2" class="btn btn-info">查询单个</button>
<button id="mybtn3" class="btn btn-info">新增</button>
<button id="mybtn4" class="btn btn-info">修改</button>
<button id="mybtn5" class="btn btn-info">删除</button>
<script>
    $(function () {
        // 演示查询所有用户
        $("#mybtn1").click(function () {
            // 发送ajax请求
            $.ajax({
                url: "${pageContext.request.contextPath}/users",
                type: "get",
                contentType: "application/json",
                success: function (res) {
                    console.log(res);
                }
            });
        });
        //演示查询单个用户
        $("#mybtn2").click(function (){
            $.ajax({
                url: "${pageContext.request.contextPath}/users/1",
                type: "get",
                contentType: "application/json",
                success: function (res) {
                    console.log(res);
                }
            });
        });

        // 演示新增用户
        $("#mybtn3").click(function () {
            // 发送ajax请求
            $.ajax({
                url: "${pageContext.request.contextPath}/users",
                type: "post",
                data: JSON.stringify({id: 10, username: "王仁康", password: "1111"}),
                contentType: "application/json",
                success: function (res) {
                    console.log(res);
                }
            });
        });
        //演示修改用户
        $("#mybtn4").click(function () {
            $.ajax({
                url: "${pageContext.request.contextPath}/users",
                type: "put",
                data: JSON.stringify({id: 10, username: "王仁康", password: "1111"}),
                contentType: "application/json",
                success: function (res) {
                    console.log(res);
                }
            });
        });
        // 演示删除用户
        $("#mybtn5").click(function () {
            // 发送ajax请求
            $.ajax({
                url: "${pageContext.request.contextPath}/users/1",
                type: "delete",
                contentType: "application/json",
                success: function (res) {
                    console.log(res);
                }
            });
        });

    });
</script>
</body>
</html>